<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Controller 路由配置/解析器
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Controller_Router
{

    /**
     * 存储单件模式实例
     *
     * @var EGP_Controller_Router
     */
    private static $_instance = null;

    /**
     * 适配主机数组
     *
     * @var array
     */
    protected $_hosts = array();

    /**
     * 单件模式调用方法
     *
     * @return EGP_Controller_Router
     */
    public static function getInstance()
    {
        if (!self::$_instance instanceof self)
            self::$_instance = new self();
        return self::$_instance;
    }

    /**
     * 构造方法
     *
     */
    final private function __construct()
    {}

    /**
     * 重置路由实例
     *
     * @return EGP_Controller_Router
     */
    public function resetInstance()
    {
        self::$_instance = new self();
        return $this;
    }

    /**
     * 获取 EGP_Controller_Front
     *
     * @return EGP_Controller_Front
     */
    public function getFrontController()
    {
        return EGP_Controller_Front::getInstance();
    }

    /**
     * 添加适配主机名
     *
     * @param  string             $httpHost
     * @param  EGP_Controller_Module  $module
     * @return EGP_Controller_Router
     * @throws EGP_Exception
     */
    public function addHost($httpHost, EGP_Controller_Module $module)
    {
        if (!preg_match('/^([a-z0-9\-]+\.?)*[a-z0-9\-]+$/i', $httpHost))
            throw new EGP_Exception("无效的主机名：'$httpHost'");
        $this->_hosts[$httpHost] = $module;
        return $this;
    }

    /**
     * 获取所有的适配主机名
     *
     * @return array
     */
    public function getHostList()
    {
        return $this->_hosts;
    }

    /**
     * 根据 URL 地址解析 Controller 参数
     *
     * @return array
     */
    public function parseParams()
    {
        $controller = $this->getFrontController();

        //分析URL
        $path = EGP_Http_Request::getInstance()->getPathInfo();
        $queryPath = $path;
        $parts = array_slice(explode('/', $path), 1, 3);
        if (count($parts) > 0) {
            foreach ($parts as $k => $v) {
                    if ($k == 0 && !empty($v)) $params['module']     = strtolower($v);
                elseif ($k == 1 && !empty($v)) $params['controller'] = $v;
                elseif ($k == 2 && !empty($v)) $params['action']     = $v;
            }
        }

        if (isset($params['module']) && $controller->hasModule($params['module'])) {
            $queryPath = substr_replace($queryPath, '', 0, strlen('/' . $params['module']));
        } else {
            $params['action'] = @$params['controller'];
            $params['controller'] = @$params['module'];
            $params['module'] = $controller->getDefaultModule();
        }
        $module = $controller->getModule($params['module']);

        //获取默认设置
        empty($params['controller']) ?
            $params['controller'] = $module->getDefaultController() :
            $queryPath = substr_replace($queryPath, '', 0, strlen('/' . $params['controller']));
        empty($params['action']) ?
            $params['action'] = $module->getDefaultAction() :
            $queryPath = substr_replace($queryPath, '', 0, strlen('/' . $params['action']));
        $queryPath == '' && $queryPath = '/';

        EGP_Http_Request::getInstance()->setQueryPath($queryPath);

        return $params;
    }

}